import Api_auth from '../../common/api/auth.js'
import {Base64} from '../../utils/common.js'

const state = {
	get UserToken(){
		return uni.getStorageSync('token')
	},
	set UserToken(value){
		uni.setStorageSync('token',value)
	},
	get USER(){
		if(uni.getStorageSync('JSON_INFO')){
			let info = decodeURI(Base64.decode(uni.getStorageSync('JSON_INFO')))
			return JSON.parse(info)
		}
		return null
	},
	set USER(value){
		if(value == null || value == ''){
		  return uni.removeStorageSync('JSON_INFO')
		}
		uni.setStorageSync('JSON_INFO',Base64.encode(encodeURI(JSON.stringify(value))))
	},
	isReg:false,
    // AppUserInfo: null,//用户信息
	/* 
		user->status  
		0  未实名 
		1  等待审核
		2  审核通过
		-1 审核未通过
		-2 封禁账号
	 */
}
const mutations = {
	SET_isReg(state, payload){
		state.isReg = payload
	},
    //vuex存入用户
    SET_USER(state, payload){
        state.USER = payload
    },
	LOGIN_IN(state,token){
		state.UserToken = token
	},
	LOGIN_OUT(state){
		state.UserToken = ''
	}
}
const actions = {
	//注册
	register({commit},payload) {
	    return new Promise((resolve, reject) => {
	        Api_auth.register(payload).then(res => {
				const {data} = res
				//触发setToken，token存入本地缓存
				commit('LOGIN_IN',data.token)
	            resolve(res)
	        }).catch(error => {
	            reject(error)
	        })
	    })
	},
	//登录
	login({commit}, payload) {
	    return new Promise((resolve, reject) => {
	        Api_auth.login(payload).then(res => {
				const {data} = res
				//触发setToken，token存入本地缓存
				commit('LOGIN_IN',data.token)
	            resolve(res)
	        }).catch(error => {
	            reject(error)
	        })
	    })
	},
	//登出
	logout({commit}, payload) {
	    return new Promise((resolve, reject) => {
	        Api_auth.logout(payload).then(res => {
				commit('LOGIN_OUT')
	            resolve(res)
	        }).catch(error => {
	            reject(error)
	        })
	    })
	},
	//注销
	logclose({commit}, payload) {
	    return new Promise((resolve, reject) => {
	        Api_auth.logclose(payload).then(res => {
				commit('LOGIN_OUT')
	            resolve(res)
	        }).catch(error => {
	            reject(error)
	        })
	    })
	},
	//用户信息
	user({commit}, payload) {
	    return new Promise((resolve, reject) => {
	        Api_auth.user(payload).then(res => {
				const {data} = res
				commit('SET_USER',data)
	            resolve(res)
	        }).catch(error => {
	            reject(error)
	        })
	    })
	},
	//绑定/更换手机号
	changeMobile({commit}, payload) {
	    return new Promise((resolve, reject) => {
	        Api_auth.changeMobile(payload).then(res => {
	            resolve(res)
	        }).catch(error => {
	            reject(error)
	        })
	    })
	},
	//修改登录密码
	updatePassword({commit}, payload) {
	    return new Promise((resolve, reject) => {
	        Api_auth.updatePassword(payload).then(res => {
	            resolve(res)
	        }).catch(error => {
	            reject(error)
	        })
	    })
	},
	//忘记密码
	forgetPassword({commit}, payload) {
	    return new Promise((resolve, reject) => {
	        Api_auth.forgetPassword(payload).then(res => {
	            resolve(res)
	        }).catch(error => {
	            reject(error)
	        })
	    })
	},
	//发送验证码
	smsSend({commit}, payload) {
	    return new Promise((resolve, reject) => {
	        Api_auth.smsSend(payload).then(res => {
	            resolve(res)
	        }).catch(error => {
	            reject(error)
	        })
	    })
	},
	//验证验证码
	smsSendOwn({commit}, payload) {
	    return new Promise((resolve, reject) => {
	        Api_auth.smsSendOwn(payload).then(res => {
	            resolve(res)
	        }).catch(error => {
	            reject(error)
	        })
	    })
	},
	//资金密码
	passwordSecond({commit}, payload) {
	    return new Promise((resolve, reject) => {
	        Api_auth.passwordSecond(payload).then(res => {
	            resolve(res)
	        }).catch(error => {
	            reject(error)
	        })
	    })
	},
	//实名认证
	certification({commit}, payload) {
	    return new Promise((resolve, reject) => {
	        Api_auth.certification(payload).then(res => {
	            resolve(res)
	        }).catch(error => {
	            reject(error)
	        })
	    })
	},
	//绑定更换手机
	changeBindMobile({commit}, payload) {
	    return new Promise((resolve, reject) => {
	        Api_auth.changeBindMobile(payload).then(res => {
	            resolve(res)
	        }).catch(error => {
	            reject(error)
	        })
	    })
	},
	
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}
